﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="iRacer">

    <title>编辑API文档接口</title>
    
    <link href="/Content/noke/css/bootstrap.min.css" rel="stylesheet">
    <link href="/Content/noke/font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="/Content/noke/js/plugins/editormd/editormd.css" rel="stylesheet" />

</head>
<body>
    <div class="row" style="margin:3px;">
        
        <div class="col-lg-2">
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h5>接口文档</h5>
                </div>
                <div class="panel-body">
                    <ul class="nav metismenu" id="doc-list"></ul>
                </div>
            </div>
        </div>

        <div class="col-lg-10">
            <div class="row">
                <div class="col-lg-4">
                    <input id="txtNo" class="form-control" type="text" style="width:280px;" disabled/>
                </div>
                <div class="col-lg-4">
                    <input id="txtIName" class="form-control" type="text" style="width:280px;" />
                </div>
                <div class="col-lg-4" style="margin:3px 0px;">
                    <div data-toggle="buttons-checkbox" class="btn-group">
                        <button class="btn btn-info" type="button" onclick="loadTemplate();"><i class="fa fa-save"></i> 加载模板</button>
                        <button class="btn btn-info" type="button" onclick="save();"><i class="fa fa-save"></i> 保存</button>
                        <button class="btn btn-info" type="button" onclick="publish();"><i class="fa fa-yelp"></i> 发布成 HTML 文件</button>
                    </div>
                    <a id="a-preview" href="javascript:void(0);" class="btn btn-success" style="display:none;" target="_blank"><i class="fa fa-search"></i> 预览</a>

                    <a href="/Home/" class="btn btn-success"><i class="fa fa-university"></i> 返回</a>

                </div>
            </div>
            <div id="test-editormd">
                <textarea style="display:none;"></textarea>
            </div>
        </div>

    </div>
    
<!-- 模板存放的地方 -->
    <div id="api-doc-templ" style="display:none">

**简要描述：** 用户注册接口

**请求URL：** ` http://xx.com/api/user/register `
  
**请求方式：** - POST 

**参数：** 

|参数名|必选|类型|说明|
|:----    |:---|:----- |-----   |
|username |是  |string |用户名   |
|password |是  |string | 密码    |
|name     |否  |string | 昵称    |

 **返回示例**

``` 
  {
    "error_code": 0,
    "data": {
      "uid": "1",
      "username": "12154545",
      "name": "吴系挂",
      "groupid": 2 ,
      "reg_time": "1436864169",
      "last_login_time": "0",
    }
  }
```

 **返回参数说明** 

|参数名|类型|说明|
|:-----  |:-----|-----                           |
|groupid |int   |用户组id，1：超级管理员；2：普通用户  |

 **备注** 

- 更多返回错误代码请看首页的错误代码描述

    </div>

    <script src="~/Content/noke/js/jquery-2.1.1.js"></script>
    <script src="~/Content/noke/js/bootstrap.min.js"></script>
    <script src="~/Content/noke/js/plugins/editormd/editormd.min.js"></script>
    <script>
        var testEditor = null;
        $(document).ready(function () {

            loadlist();

            testEditor = editormd("test-editormd", {
                height: 1000,
                syncScrolling: "single",
                path: '/Content/noke/js/plugins/editormd/lib/'
            });
        });

        function loadlist() {

            var url = '/Doc/GetInterfaceDocList/';
            $.getJSON(url, function (res) {
                if (res.Code == 100 && res.Value && res.Value.length > 0) {
                    var sHtml = '';
                    $(res.Value).each(function (i, item) {
                        sHtml += '<li class="special_link"> <a href="javascript:load(' + item.No + ');"> <i class="fa fa-file"></i> <span class="nav-label">' + item.IName + '</span> </a></li>';
                    });
                    $('#doc-list').html(sHtml);
                } else {
                    console.log('Get List:' + res.Msg);
                }
            }, 'json');



        };

        function load(no) {
            var url = '/Doc/GetInterfaceDoc/' + no;
            $.getJSON(url, function (res) {
                if (res.Code == 100 && res.Value) {
                    $('#txtNo').val(res.Value.No)
                    $('#txtIName').val(res.Value.IName)
                    testEditor.setValue(res.Value.Markdown);
                    $('#a-preview').attr('href', '/content/document/api/' + res.Value.No + '.html').show();
                } else {
                    console.log('Get:' + res.Msg);
                }
            }, 'json');
        }

        function loadTemplate() {
            $('#txtNo').val('');
            $('#txtIName').val('');
            var tmpl = $("#api-doc-templ").html();
            testEditor.insertValue(tmpl);
            $('#a-preview').hide();
        }

        function save() {

            var model = { No: $('#txtNo').val(), IName: $('#txtIName').val(), Markdown: testEditor.getMarkdown(), HtmlText: '' }, isNew = false;
            if (model.No == '') {
                model.No = (new Date().getTime()).toString();
                $('#txtNo').val(model.No);
                isNew = true;
            }

            var url = '/Doc/SaveInterfaceDoc/';

            $.post(url, model, function (res) {
                if (res.Code == 100) {
                    if (isNew)
                        loadlist();
                    alert("Save success !!!");
                } else {
                    console.log('Save: ' + res.Msg);
                }
            }, 'json');
        }

        function publish() {
            var model = { no: $('#txtNo').val(), html: testEditor.getPreviewedHTML() };
            if (model.no == '') {
                alert("请先保存，后发布！");
                return false;
            }
            model.html =encodeURIComponent(model.html);
            var url = '/Doc/Publish/';

            $.post(url, model, function (res) {
                if (res.Code == 100) {
                    $('#a-preview').attr('href', '/content/document/api/' + model.no + '.html').show();
                    alert("Publish success !!!");
                } else {
                    console.log('Save: ' + res.Msg);
                }
            }, 'json');

            
        }

    </script>

</body>
</html>
